package com.hgx.su.common.third.random;


import org.apache.commons.lang3.RandomStringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用于生成各种随机数
 *
 * @author sandy
 */
public class RandomUtil {

    /**
     * 生成指定位数的数字
     *
     * <pre>
     * length < 15 : 指定长度数字：000000000
     * length >=15 : yyyyMMddHHmmss&随机字符串：202001012459590000
     * </pre>
     *
     * @return
     */
    public static String createNumeric(int len) {
        if (len < 15) {
            return RandomStringUtils.randomNumeric(len);
        }
        String now = getCurrentDate();
        String number = RandomStringUtils.randomNumeric(len - now.length());
        return now + number;
    }

    /**
     * 生成18位的数字
     *
     * @return
     */
    public static String createNumeric() {
        String now = getCurrentDate();
        String number = RandomStringUtils.randomNumeric(4);
        return now + number;
    }

    /**
     * 生成小于20位的随机字符串 (yyyyMMddHHmmss&6位随机数:20200101245959TPCBpv)
     *
     * @return
     */
    public static String createRandomNum() {
        String now = getCurrentDate();
        String number = RandomStringUtils.random(6, true, true);
        return now + number;
    }

    /**
     * 创建length长度的随机字符串
     *
     * <pre>
     * length < 15 : 指定数量字符串：zRG9AAdcSfb7
     * length >=15 : yyyyMMddHHmmss&随机字符串：20200101245959Jai9
     * </pre>
     *
     * @param length
     * @return
     */
    public static String createRandomNum(int length) {
        if (length < 15) {
            return RandomStringUtils.random(length, true, true);
        }
        String now = getCurrentDate();
        String number = RandomStringUtils.random(length - now.length(), true, true);
        return now + number;
    }

    private static String getCurrentDate() {
        return new SimpleDateFormat(FULL_SPECIAL_FORMAT).format(new Date());
    }

    public static final String FULL_SPECIAL_FORMAT = "yyyyMMddHHmmss";

}
